<template>
  <div>
    <el-dialog
      :title="'每日工作内容'"
      :visible.sync="workOpen"
      width="500px"
      :height="height || '450px'"
      :before-close="handleClose"
      append-to-body
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="任务名称" prop="questName">
          <el-select v-model="form.questName" disabled :size="medium" placeholder="请选择" style="width: 100%">
          </el-select>
        </el-form-item>
        <el-form-item label="每日工作" prop="workArrangement">
          <el-input v-model="form.workArrangement" disabled :size="medium" type="textarea" :rows="4" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="协调工作" prop="workCoordinate">
          <el-input v-model="form.workCoordinate" :size="medium" disabled type="textarea" :rows="4" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="汇报时间" prop="createTime" >
          <el-date-picker
            v-model="form.createTime"
            disabled
            type="date"
            :size="medium"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="填写人" prop="createByName" style="width: 65%">
          <el-input
            disabled
            v-model="form.createByName"
            readonly
            type="text"
            clearable
            :size="medium"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">复 制</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

  <script>
import Treeselect from "@riophae/vue-treeselect";
import {getWorkSchedule} from "@/api/system/mis/workSchedule";
import {shareData} from "@/api/system/mis/share";

export default {
  name: "PeopleSelect",
  components: { Treeselect },
  dicts: ["sys_normal_disable", "sys_user_sex"],
  props: {
    width: {
      type: String,
      default: "900px",
    },
    height: {
      type: String,
      default: "650px",
    },
    type: {
      type: String,
      default: "single", //single or multiple
    },
    id: {
      type: String,
      default: "",
    },
    workOpen: {
      type: Boolean,
      default: false,
    },
  },
  computed: {},
  data() {
    return {
      loading: false,
      form:{},
      workOpen: false,
      activeName: "first",
      defaultProps: {
        children: "children",
        label: "label",
      },
      // 部门树选项
      deptOptions: undefined,
      deptName: "",
      showSearch: true,
      medium:'medium',
      userList: [],
      allUserList: [],
      allUserMap: new Map(),
      selectedUserList: [],

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 2147483647,
        userName: undefined,
        phonenumber: undefined,
        status: "0",
        deptId: undefined,
      },

      //已选择的用户信息
      checkedUsers: [],

      //   userNodeAll: false,
      //   cancelUserNodeAll: false,
    };
  },

  created() {
    this.queryData();
  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val);
    },
    id(to, from) {
      this.queryData();
    }
  },
  methods: {
    handleClose(done) {
      this.cancel();
    },
    handleClick(tab, event) {
      console.log(tab, event);
    },

    submitForm() {
      this.form.type = 'workDay'
      let jsonRow = JSON.stringify(this.form);
      shareData(jsonRow).then(response => {
        let textarea = document.createElement("textarea");
        // 设置textarea的值为要复制的文本
        textarea.value = response;
        // 将textarea元素添加到DOM中
        document.body.appendChild(textarea);
        // 选中textarea中的文本
        textarea.select();
        // 复制文本到剪贴板
        document.execCommand("copy");
        // 删除textarea元素
        document.body.removeChild(textarea);
        this.$message({
          message: "复制成功",
          type: "success",
        });
        this.cancel()
      })
    },
    cancel() {
      this.$emit("cancel");
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      // 判断当前节点是否已选中
      if (this.isNodeChecked(data)) {
        // 如果已选中，取消选择
        this.uncheckNode(data);
      } else {
        // 如果未选中，选择当前节点及其所有子节点
        this.checkNode(data);
      }
    },
    isNodeChecked(node) {
      // 判断节点是否已选中
      return this.userList.some((item) => item.id === node.id);
    },
    checkNode(node) {
      // 选择当前节点及其所有子节点
      this.userList.push(node);
      if (node.children && node.children.length > 0) {
        node.children.forEach((childNode) => {
          this.checkNode(childNode);
        });
      }
    },
    uncheckNode(node) {
      // 取消选择当前节点及其所有子节点
      this.userList = this.userList.filter((item) => item.id !== node.id);
      if (node.children && node.children.length > 0) {
        node.children.forEach((childNode) => {
          this.uncheckNode(childNode);
        });
      }
    },
    /** 查询部门下拉树结构 */
    queryData() {
      if (this.id == ''){
        return
      }
      this.form ={}
      getWorkSchedule(this.id).then(response => {
        this.form = response.data;
      });
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.deptId = undefined;
      this.$refs.tree.setCurrentKey(null);
      this.getList();
    },

    handleCheckedUsersChange(val) {
      // console.log(this.$refs.peopleCheckBoxes.value);
      this.updateCheckedUsers();
    },

    updateCheckedUsers() {
      this.selectedUserList = [];

      this.checkedUsers.forEach((item) => {
        if (this.allUserMap.has(item)) {
          let u = this.allUserMap.get(item);
          this.selectedUserList.push(u);
        }
      });
    },

    deleteUserByUserName(username) {
      for (let i = 0; i < this.checkedUsers.length; i++) {
        if (this.checkedUsers[i].indexOf(username) != -1) {
          this.checkedUsers.splice(i, 1);
          this.updateCheckedUsers();
          break;
        }
      }
    },

    handleCheckedNodeAll(val) {
      if (val) {
        //全选
        this.userList.forEach((item) => {
          for (let i = 0; i < this.checkedUsers.length; i++) {
            if (this.checkedUsers[i] == item.userName) {
              //已存在，跳过
              return;
            }
          }
          //不存在，执行添加
          this.checkedUsers.push(item.userName);
        });
      } else {
        //全不选
        this.userList.forEach((item) => {
          for (let i = 0; i < this.checkedUsers.length; i++) {
            if (this.checkedUsers[i] == item.userName) {
              //已存在，执行删除
              this.checkedUsers.splice(i, 1);
              return;
            }
          }
          //不存在，无需操作
        });
      }
      this.updateCheckedUsers();
    },

    clearAll() {
      this.checkedUsers = [];
      this.updateCheckedUsers();
    },
  },
};
</script>

  <style lang="scss" scoped>
.selectBox {
  width: 100%;
  min-width: 900px;
  display: flex;
  flex-direction: column;

  .topBox {
    width: 100%;
    display: flex;
    margin-top: 10px;
    .leftBox {
      width: 425px;
      height: 400px;
      margin-right: 10px;

      -webkit-box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.12),
        0 0 6px 0 rgba(0, 0, 0, 0.04);
      box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.12), 0 0 6px 0 rgba(0, 0, 0, 0.04);
      display: flex;
      flex-direction: column;

      .leftBox_title {
        width: 100%;
        height: 40px;
        font-size: 16px;
        line-height: 40px;
        padding-left: 10px;
        background-color: #f5f5f5;
        position: relative;
        .leftBox_title_do {
          position: absolute;
          right: 16px;
          top: 8px;
        }
      }

      .contentBox {
        width: 100%;
      }
    }
  }

  .bottomBox {
    margin-top: 15px;
    width: 100%;
    height: 75px;
    // border: 1px solid #DCDFE6;
    // -webkit-box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.12), 0 0 6px 0 rgba(0, 0, 0, 0.04);
    // box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.12), 0 0 6px 0 rgba(0, 0, 0, 0.04);
    // overflow-y: auto;

    .selectPeopleList {
      width: 100%;
      display: flex;
      flex-wrap: wrap;

      .selectPeopleCard {
        width: 50px;
        height: 56px;
        display: flex;
        flex-direction: column;
        align-items: center;
        margin: 5px 10px;
        .user-avatar {
          width: 40px;
          height: 40px;
          border-radius: 50%;
          color: #fff;
          background: #ffa502;
          font-size: 14px;
          text-align: center;
          line-height: 40px;
        }

        .userInfo {
          width: 100%;
          display: flex;
          flex-direction: column;
          margin-left: 5px;
          margin-right: 5px;

          div:nth-child(1) {
            font-size: 14px;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            width: 100%;
            text-align: center;
          }
        }
      }
    }
  }
}

.treeBox {
  width: 100%;
  height: 320px;
  overflow-x: hidden;
  overflow-y: auto;
}

.peopleBox {
  width: 100%;
  height: 360px;
  overflow-x: hidden;
  overflow-y: auto;

  .peopleList {
    width: 100%;
    height: auto;

    .peopleCard:hover {
      background-color: #fafafa;
      transition: 0.2s;

      .peopleName {
        color: #eb6100;
      }
    }
    .peopleCard {
      width: 100%;
      height: 48px;
      border-top: solid #f5f5f5 1px;
      border-bottom: solid #f5f5f5 1px;
      padding-left: 8px;
      display: flex;
      align-items: center;
      cursor: pointer;
      transition: 0.2s;
      ::v-deep .el-checkbox__label {
        display: flex;
        align-items: center;
      }

      .avatarBox {
        width: 40px;
        height: 40px;

        .user-avatar {
          width: 40px;
          height: 40px;
          border-radius: 50%;
          color: #fff;
          font-size: 16px;
          text-align: center;
          line-height: 40px;
          user-select: none;
        }
      }

      .peopleInfoBox {
        width: 190px;
        height: 100%;
        display: flex;
        flex-direction: column;
        padding-left: 8px;

        .peopleName {
          width: 100%;
          height: 20px;
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis;
          font-size: 16px;
          line-height: 20px;
        }

        .peopleDept {
          width: 100%;
          height: 20px;
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis;
          font-size: 14px;
          line-height: 20px;
          color: #999;
        }
      }

      .peopleDeleteBtn {
        width: 20px;
        height: 20px;
        // border-radius: 50%;
        // background: url(./delete2.png) no-repeat;
        background-size: 100% 100%;
        transition: 0.2s;
      }

      .peopleDeleteBtn:hover {
        width: 20px;
        height: 20px;
        // border-radius: 50%;
        // background: url(./delete1.png) no-repeat;
        background-size: 100% 100%;
        transition: 0.2s;
      }
    }
  }
}
</style>
